<template>
  <div class="main">
    <van-sticky>
      <div class="nav">
        <img :src="back" alt="" @click="handleBack"/>
        <div style="font-size: 20px;font-weight: 500;">设置</div>
      </div>
    </van-sticky>
    <div class="body">
      <van-cell title="账号安全" is-link @click="gotoAccountSecurity"/>
      <van-cell title="清除缓存" is-link :value="cache" @click="gotoClearCache"/>
      <!--      <van-cell title="推送通知配置" is-link value=""/>-->
      <!--      <van-cell title="资质证照" is-link value=""/>-->
      <van-cell title="用户服务协议" is-link value="" @click="goUserService"/>
      <van-cell title="隐私服务条款" is-link value="" @click="goPrivacyTermsService"/>
      <van-cell title="关于百合医" is-link value="" @click="goAbout"/>
      <van-cell title="检测新版本" :value="versionName"/>
      <van-cell title="上传日志" is-link value=""/>
      <!--      <van-cell title="意见反馈" is-link value="" @click="gotoFreeBack"/>-->
      <van-cell title="" value=""/>
    </div>
    <div class="footer">
      <div class="size" @click="logout">退出登录</div>
    </div>
    <!--    <el-button size="mini" @click="accountOrSecurity">账号与安全</el-button>-->
    <!--    <el-button size="mini" @click="freeBack">意见反馈</el-button>-->
  </div>
</template>

<script>
import {checkUpdate} from '@/api/my'
import Cookies from "js-cookie";
import {Dialog} from 'vant';

export default {
  name: "index",
  data() {
    return {
      back: require("@/assets/my/back.png"),
      setImg: require("@/assets/my/set-icon.png"),
      versionList: {},
      versionName: '',
      cache: ''
    }
  },
  mounted() {
    if (Cookies.get('cacheSize')) {
      this.cache = (Cookies.get('cacheSize') / (1024*1024)).toFixed(2) + 'M'
    }
    if (Cookies.get('version')) {
      this.versionName = 'v' + Cookies.get('version')
    }
    // this.getVersion()
    // window["getInfoFromFlutter"] = (info) => {
    //   if (JSON.parse(info).action === 'clearCache') {
    //     this.getInfoFromFlutter(info);
    //   }
    // };
  },
  methods: {
    gotoClearCache() {
      Dialog.confirm({
        title: '提示',
        message: '您确定要清除缓存吗？',
        confirmButtonColor: 'rgba(242, 150, 0, 1)'
      }).then(() => {
        this.clearCache()
        Cookies.set('cacheSize', '0.00M')
        // this.$nextTick(()=>{
        //   this.cache = (Cookies.get('cacheSize') / (1024*1024)).toFixed(2) + 'M'
        // })
      }).catch(err => {
      })
    },
    logout() {
      Dialog.confirm({
        title: '提示',
        message: '您确定要退出登录吗？',
        confirmButtonColor: 'rgba(242, 150, 0, 1)'
      }).then(() => {
        this.sureLogOut()
      }).catch(err => {

      })
    },
    sureLogOut() {
      let message = {
        action: 'logout',
        param: {
          type: "logout",            //必填
        }
      }
      getInfoFromVue.postMessage(JSON.stringify(message))
    },
    clearCache() {
      let message = {
        action: 'clearCache',
      }
      getInfoFromVue.postMessage(JSON.stringify(message))
    },
    getVersion() {
      let data = {
        sysType: 1,//1安卓,2ios
        versionCode: 1
      }
      checkUpdate(data).then(res => {
        this.versionList = {...res.data}
        console.log('+++', this.versionList)
      })
    },
    goUserService() {
      this.$router.push(
          {
            path: '/userServiceAgreement',
            query: {}
          }
      )
    },
    goPrivacyTermsService() {
      this.$router.push(
          {
            path: '/privacyTermsService',
            query: {}
          }
      )
    },
    goAbout() {
      this.$router.push(
          {
            path: '/about',
            query: {}
          }
      )
    },
    handleBack() {
      let message = {
        action: 'showBottom',
        param: {}
      }
      getInfoFromVue.postMessage(JSON.stringify(message))
      // this.$router.go(-1);
      this.$router.push({path: '/my'})
    },
    gotoAccountSecurity() {
      this.$router.push(
          {
            path: '/accountOrSecurity',
            query: {}
          }
      )
    },
    gotoFreeBack() {
      this.$router.push(
          {
            path: '/freeBack',
            query: {}
          }
      )
    },
  }
}
</script>

<style scoped lang="less">
.main {
  height: 100vh;
  padding: 0px 15px;

  .nav {
    height: 44px;
    display: flex;
    justify-content: center;
    align-items: center;

    > img {
      width: 10px;
      height: 16px;
      position: absolute;
      left: 15px;
    }
  }

  .body {
    padding-top: 30px;

    /deep/ .van-cell__title {
      font-size: 17px;
      font-weight: 400;
    }
  }

  .footer {
    display: flex;
    justify-content: center;
    margin-top: 50px;

    .size {
      width: 141px;
      height: 38px;
      //opacity: 1;
      border-radius: 19px;
      background: rgba(242, 150, 0, 1);
      font-size: 23px;
      font-weight: 500;
      //line-height: 38px;
      color: rgba(255, 255, 255, 1);
      //text-align: center;
      //vertical-align: top;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 3px;
    }
  }

  /deep/ .van-dialog__message {
    font-size: 16px;
  }
}
</style>
